#pragma once
#include<assert.h>

namespace zxj
{
	template<class T>
	struct less
	{
		bool operator()(T x, T y)
		{
			return x < y;
		}
	};

	template<class T>
	struct greater
	{
		bool operator()(T x, T y)
		{
			return x > y;
		}
	};


	template<class T,class container=vector<T>,class com=less<T>>
	class priority_queue
	{
	public:

		void adjustdown(size_t parent)
		{
			com _com;
			size_t child = parent * 2 + 1;
			while (child < _con.size())
			{
				if (child + 1 < _con.size() && _com(_con[child], _con[child + 1]))
				{
					++child;
				}

				if (_com(_con[parent], _con[child]))
				{
					std::swap(_con[parent], _con[child]);
					parent = child;
					child = parent * 2 + 1;
				}
				else
				{
					break;
				}
			}
		}

		void adjustup(size_t child)
		{
			size_t parent = (child - 1) / 2;
			while (child > 0)
			{
				com _com;
				if (_com(_con[parent], _con[child]))
				{
					std::swap(_con[parent], _con[child]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}

		void pop()
		{
			assert(!empty());

			std::swap(_con[0], _con[size() - 1]);
			_con.pop_back();
			adjustdown(0);
		}

		void push(const T& x)
		{
			_con.push_back(x);
			adjustup(size() - 1);
		}

		T& top()
		{
			return _con[0];
		}

		const T& top()const
		{
			return _con[0];
		}

		size_t size()
		{
			return _con.size();
		}

		bool empty()
		{
			return _con.empty();
		}

	private:
		container _con;
	};

	void TestPriorityQueue()
	{
		// 默认情况下，创建的是大堆，其底层按照小于号比较
		vector<int> v{3, 2, 7, 6, 0, 4, 1, 9, 8, 5};
		priority_queue<int> q1;
		for (auto& e : v)
			q1.push(e);
		cout << q1.top() << endl;

		priority_queue<int,vector<int>,greater<int>>q2;
		for (auto& e : v)
			q2.push(e);
		cout << q2.top() << endl;
	}
}